import 'dart:async';
import 'package:qgchat/bloc/blocProvider.dart';
import 'package:qgchat/http/apis/video.dart';
import 'package:qgchat/http/models/shortVideoModel.dart';

class PopularBloc implements BlocBase {

  PopularBloc() {
    getRecommendList();
  }

  /// 分页
  int _page = 1;

  /// 分页数
  int _limit = 6;

  /// 热门主播列表
  List<ShortVideoModel> hotlist = [];


  // ##########  STREAMS  ##############

  /// 最热门主播stream
  StreamController<List<ShortVideoModel>> _recommendController = StreamController.broadcast();
  Sink get _callSink => _recommendController.sink;
  Stream get callStream => _recommendController.stream;

  void dispose(){
    _recommendController.close();
  }

  // ##########  HANDLING  ##############

  /// 获取推荐主播列表数据
  Future getRecommendList() async {
    List<ShortVideoModel> result = await VideoApis.popluar(page:_page,limit: _limit);
    hotlist.addAll(result);
    if (!_recommendController.isClosed) {
      _callSink.add(hotlist);
    }
  }

  /// 重新加载页面
  Future onRefresh() async {
    _page = 1;
    hotlist.clear();

    await getRecommendList();
  }

  /// 加载更多
  Future onLoad() async {
    _page++;
    await getRecommendList();
  }

}